package org.wenhuayuan.project.main.fragment.home

import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import org.wenhuayuan.library.resful.HiCallback
import org.wenhuayuan.library.resful.HiResponse
import org.wenhuayuan.project.common.view.HiAbsListFragment
import org.wenhuayuan.project.main.http.ApiFactory
import org.wenhuayuan.project.main.http.api.HomeApi
import org.wenhuayuan.project.main.model.HomeModel
import org.wenhuayuan.uilibrary.hiitem.HiDataItem

class HomeTabFragment : HiAbsListFragment() {
    private val DEFAULT_HOT_TAB_ATEGORY_ID = "1"
    private var categoryId:String? = null
    companion object {
        fun newInstance(categoryId: String): HomeTabFragment {
            val arg = Bundle()
            arg.putString("categoryId", categoryId)
            val fragment = HomeTabFragment()
            fragment.arguments = arg
            return fragment
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        categoryId = arguments?.getString("categoryId", DEFAULT_HOT_TAB_ATEGORY_ID)
        super.onViewCreated(view, savedInstanceState)

        queryTabCategoryList(categoryId!!)

        enableLoadMore { queryTabCategoryList(categoryId!!) }
    }

    override fun onRefresh() {
        super.onRefresh()
        queryTabCategoryList(categoryId!!)
    }

    private fun queryTabCategoryList(categoryId:String) {
        ApiFactory.create(HomeApi::class.java).
            queryTabCategoryList(categoryId, pageIndex, 10)
            .enqueue(object:HiCallback<HomeModel>{
            override fun onSuccess(response: HiResponse<HomeModel>) {
                Log.e("HomeTabFragment", ""+response)
                if (response.successful()) {
                    updateUI(response.data)
                } else {
                    // 空数据页面
                    finishRefresh(null)
                }
            }

            override fun onFailed(throwable: Throwable) {
                // 空数据页面
                finishRefresh(null)
            }
        })
    }

    private fun updateUI(data: HomeModel?) {
        Log.e("HomeTabFragment", "=== updateUI ===")
        if (!isAlive) return
        data?.let {homeModel ->
            val dataItems = mutableListOf<HiDataItem<*, RecyclerView.ViewHolder>>()
            homeModel.bannerList?.let {
                dataItems.add(BannerItem(it))
            }
            homeModel.subcategoryList?.let {
                dataItems.add(GridItem(it))
            }
            homeModel.goodsList?.forEachIndexed { _, goodsModel ->
                dataItems.add(GoodsItem(goodsModel, TextUtils.equals(categoryId, DEFAULT_HOT_TAB_ATEGORY_ID)))
            }
            finishRefresh(dataItems)
        }
    }

    override fun createLayoutManager(): RecyclerView.LayoutManager {
        val isHotTab = TextUtils.equals(categoryId, DEFAULT_HOT_TAB_ATEGORY_ID)
        return if (isHotTab) super.createLayoutManager() else GridLayoutManager(context, 2)
    }
}